<?php

use think\migration\Migrator;

class ChatMessage extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    protected string $name = 'chat_message';

    public function up(): void
    {
        /*
CREATE TABLE `test_chat_message` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `type` varchar(30) NOT NULL DEFAULT '' COMMENT '对话类型',
  `user_id` int(11) NOT NULL COMMENT '用户ID',
  `session_id` int(11) NOT NULL COMMENT '会话ID',
  `channel_id` int(11) NOT NULL COMMENT '通道ID',
  `is_first` tinyint(1) NOT NULL DEFAULT '1' COMMENT '首轮对话',
  `send_message` text COMMENT '发送内容',
  `send_at` datetime NOT NULL COMMENT '发送时间',
  `send_ip` varchar(30) NOT NULL DEFAULT '' COMMENT '发送者IP',
  `receive_message` text COMMENT '接收内容',
  `receive_at` datetime NOT NULL COMMENT '接收时间',
  `send_tokens` int(11) NOT NULL DEFAULT '0' COMMENT '发送token量',
  `receive_tokens` int(11) NOT NULL DEFAULT '0' COMMENT '接收token量',
  `total_tokens` int(11) NOT NULL DEFAULT '0' COMMENT '总计token量',
  PRIMARY KEY (`id`),
  KEY `user_session` (`user_id`,`session_id`,`channel_id`,`id`),
  KEY `chat_type` (`type`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
         */
        $table = $this->table($this->name, [
            'comment'     => '对话记录',
            'engine'      => 'InnoDB',
            'collation'   => 'utf8mb4_general_ci',
            'id'          => 'id',
            'signed'      => false,
            'primary_key' => ['id']
        ]);
        $table->addColumn('user_id', 'integer', ['limit' => 11, 'signed' => false, 'null' => false, 'comment' => '用户ID'])
            ->addColumn('object_type', 'string', ['limit' => 30, 'null' => false, 'comment' => '对话类型'])
            ->addColumn('object_id', 'integer', ['limit' => 11, 'signed' => false, 'null' => false, 'comment' => '对话ID'])
            ->addColumn('channel_id', 'integer', ['limit' => 11, 'signed' => false, 'null' => false, 'comment' => '通道ID'])
            ->addColumn('session', 'string', ['limit' => 32, 'null' => false, 'comment' => '会话标识'])
            ->addColumn('key_id', 'integer', ['limit' => 11, 'default' => 0, 'null' => false, 'comment' => '平台密钥ID'])
            ->addColumn('index', 'tinyinteger', ['limit' => 3, 'signed' => false, 'default' => 1, 'null' => false, 'comment' => '对话论数'])
            ->addColumn('prompt_text', 'text', ['null' => false, 'comment' => '发送内容'])
            ->addColumn('send_at', 'datetime', ['null' => false, 'comment' => '发送时间'])
            ->addColumn('send_ip', 'string', ['limit' => 30, 'null' => false, 'default' => '', 'comment' => '发送者IP'])
            ->addColumn('carry_num', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '携带历史对话轮数'])
            ->addColumn('receive_status', 'string', ['limit' => 20, 'default' => '', 'comment' => '接收状态'])
            ->addColumn('receive_text', 'text', ['null' => true, 'comment' => '接收内容'])
            ->addColumn('receive_errcode', 'string', ['limit' => 50, 'null' => true, 'comment' => '接收错误码'])
            ->addColumn('receive_at', 'datetime', ['null' => true, 'comment' => '接收时间'])
            ->addColumn('send_tokens', 'integer', ['limit' => 11, 'null' => true, 'comment' => '发送token量'])
            ->addColumn('receive_tokens', 'integer', ['limit' => 11, 'null' => true, 'comment' => '接收token量'])
            ->addColumn('total_tokens', 'integer', ['limit' => 11, 'null' => true, 'comment' => '总计token量'])
            ->addIndex(['user_id', 'session', 'channel_id', 'id'], ['name' => 'user_session', 'type' => 'index'])
            ->addIndex(['object_type', 'id'], ['name' => 'chat_type', 'type' => 'index'])
            ->save();

    }

    public function down(): void
    {
        $table = $this->table($this->name);
        $table->exists() && $table->drop()->save();
    }
}
